package ljl.alg.wangzheng_camp.round1.dp;

public class _714_profit {
    
    // 我大概就是这个意思但是不会写
    public int maxProfit(int[] prices, int fee) {
        int[][] dp = new int[prices.length][2];
        dp[0][0] = 0;
        dp[0][1] = -prices[0];
        for (int i = 1; i < prices.length; i++) {
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i] - fee);
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
        }
        return dp[prices.length - 1][0];
    }
    
    /*
    * 牛逼啊 短短几行，解决了动态规划问题
    * */
    public int maxProfit2(int[] prices, int fee) {
        int nil = 0, bought = -prices[0];
        for (int i = 1; i < prices.length; i++) {
            nil = Math.max(nil, bought + prices[i] - fee);
            bought = Math.max(bought, nil - prices[i]);
        }
        return nil;
    }
    
    public int maxProfit3(int[] prices, int fee) {
        int buy = prices[0] + fee;
        int profit = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] + fee < buy)
                buy = prices[i] + fee;
            else if (prices[i] > buy) {
                profit += prices[i] - buy;
                buy = prices[i];
            }
        }
        return profit;
    }
    
}
